package com.oazon.moco.serialport;

import java.io.File;
import java.nio.ByteBuffer;




import com.moco.launcher.link.LinkManager;
import com.oazon.common.Logger;
import com.oazon.moco.serialport.core.Pack;
import com.oazon.moco.serialport.core.Serial;
import com.oazon.moco.serialport.core.Type;


public class SerialSend {
	private static Serial serial;
	private static SerialSend serialSend;
	private SerialSend(){
		serial=Serial.getInstance();
	}
	
	public static SerialSend getInstance(){
		if(serialSend==null){
			serialSend=new SerialSend();
		}
		return serialSend;
	}

	public static final int Serial_HARDWAREVERSION=1;
	public static final int Serial_SHUTDOWN=2;
	public static final int Serial_SHUTDOWNTOOPEN_TIME=3;
	public static final int Serial_SHUTDOWNTOOPEN_TIME_CANCEL=4;
	public static final int Serial_SLEEPING=5;
	public static final int Serial_WAKE=6;
	public static final int Serial_OPEN=7;
	public static final int Serial_CHOOSEEQ=8;
	public static final int Serial_FILEEQ=9;
	public static final int Serial_VOLUEM_NUMBER=10;
	public static final int Serial_POWER_CHARGE=11;
	public static final int Serial_POWER_FULL=12;
	public static final int Serial_POWER_PERCENT=13;
	public static final int Serial_POWER_EXTERNAL=14;
	public static final int Serial_DISTANCE=15;
	public static final int Serial_ANSWER=16;
    public static final int Serial_WATCHDOG_TIMER=17;
    public static final int Serial_BOOTLOADER=18;
    
    //michael add
    public static final int Serial_WATCHDOG_OPEN=19;
    public static final int Serial_WATCHDOG_CLOSE=20;
    public static final int Serial_SCREEN_PROTECT=21;//屏保
    public static final int Serial_GET_BATTERY_VOLTAGE=22;//获取电压值

	public void sendStatus(int j){
		Logger.d("SerialSend", "type=="+j);
		Pack p=new Pack();
		switch (j) {
		case Serial_HARDWAREVERSION:
			p.setType(Type.CMD_GET_VERSION);
			serial.send(p);
			break;
		case Serial_SHUTDOWN:
			p.setType(Type.CMD_NOTIFY_HOST_OFF_SOON);
			serial.send(p);
			break;
		case Serial_SHUTDOWNTOOPEN_TIME:
			p.setType(Type.S_SHUTDOWNTOOPEN_TIME);
         	byte[] b3=Pack.intToByteArray(10000);
         	p.setData(b3);
			serial.send(p);
			break;
		case Serial_SHUTDOWNTOOPEN_TIME_CANCEL:
			p.setType(Type.S_SHUTDOWNTOOPEN_TIME_CANCEL);
			serial.send(p);
			break;
		case Serial_SLEEPING:
			p.setType(Type.CMD_NOTIFY_HOST_GO_STANDBY);
			serial.send(p);
			break;
		case Serial_WAKE:
			p.setType(Type.CMD_NOTIFY_HOST_GO_NORMAL);
			serial.send(p);
			break;
		case Serial_OPEN:
			p.setType(Type.CMD_NOTIFY_HOST_BOOT_OK);
			serial.send(p);
			break;
		case Serial_CHOOSEEQ:
			p.setType(Type.S_CHOOSEEQ);
         	byte[] b8=new byte[1];
         	b8[0]=Type.EQ02;
         	p.setData(b8);
			serial.send(p);
			break;
		case Serial_FILEEQ:
		 	p.setType(Type.S_FILEEQ);
         	p.setData(Type.EQFile1);
			serial.send(p);
			break;
		case Serial_VOLUEM_NUMBER:
			p.setType(Type.CMD_SET_MASTER_VOL);
         	p.setData(Pack.intToBytes2(0));
			serial.send(p);
			break;
		case Serial_POWER_CHARGE:
			p.setType(Type.CMD_GET_IS_BAT_CHARGING);
			serial.send(p);
			break;
		case Serial_POWER_FULL:
			p.setType(Type.S_POWER_FULL);
			serial.send(p);
			break;
		case Serial_POWER_PERCENT:
			p.setType(Type.CMD_GET_BAT_LEVEL);
			serial.send(p);
			break;
		case Serial_POWER_EXTERNAL:
			p.setType(Type.CMD_GET_IS_DC_IN);
			serial.send(p);
			break;
		case Serial_DISTANCE:
			p.setType(Type.S_DISTANCE);
         	byte[] b15=new byte[1];
         	b15[0]=1;
         	p.setData(b15);
			serial.send(p);
			break;
		case Serial_ANSWER:
			p.setType(Type.SM_ANSWER);
         	byte[] b=new byte[1];
         	b[0]=1;
         	p.setData(b);
			serial.send(p);
			break;
		case Serial_WATCHDOG_TIMER:
			p.setType(Type.S_WATCHDOG_TIMER);
			serial.send(p);
			break;
		case Serial_BOOTLOADER:
			p.setType(Type.CMD_SET_GO_BOOTLOADER);
			serial.send(p);
			break;
		case Serial_WATCHDOG_OPEN:
			p.setType(Type.S_WATCHDOG);
         	byte[] opendog= new byte[2];
         	opendog[0] = (byte)LinkManager.getInstance().getSaveSystemState().ordinal();
         	opendog[1] = 0x01;
         	p.setData(opendog);
			serial.send(p);
			break;
		case Serial_WATCHDOG_CLOSE://TODO: refactor serial send interface
			p.setType(Type.S_WATCHDOG);
			byte[] closedog= new byte[2];
			closedog[0] = (byte)LinkManager.getInstance().getSaveSystemState().ordinal();
			closedog[1] = 0x00;
         	p.setData(closedog);
			serial.send(p);
			break;
		case Serial_SCREEN_PROTECT:
			p.setType(Type.S_SCREEN_PROTECT);
			serial.send(p);
			break;
		case Serial_GET_BATTERY_VOLTAGE:
			p.setType(Type.CMD_GET_BATTERY_VOLTAGE);
			serial.send(p);
			break;
		default:
			break;
		}
	}
	
	public void sendTime(int time){
		Pack p=new Pack();
		p.setType(Type.S_SHUTDOWNTOOPEN_TIME);
		p.setData(Pack.int2byteArray(time));
		serial.send(p);
	}
	
	public void sendVolume(int volume){
		 Pack p=new Pack();
		 p.setType(Type.CMD_SET_MASTER_VOL);
		 int a=0x00+volume;
		 ByteBuffer buffer=ByteBuffer.allocate(1);
		 buffer.put((byte)a);
		 p.setData(buffer.array());
		 serial.send(p);
	}
	
	public void ChooseEQ(byte b){
		 Pack p=new Pack();
		p.setType(Type.S_CHOOSEEQ);
     	byte[] b8=new byte[1];
     	b8[0]=b;
     	p.setData(b8);
		serial.send(p);
	}
	
	public void sendEQFile(int[] eq_h,int[] eq_l){
		Pack p=new Pack();
		p.setType(Type.S_FILEEQ);
     	p.setData(Pack.getEQ(eq_h, eq_l));
		serial.send(p);
	}

     public void close(){
		serial.closeSerialPort();
		
	}
}
